package com.geovis.emergency.spd.controller.basic;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.constant.CommonConstants;
import com.geovis.common.core.constant.SwaggerConstants;
import com.geovis.common.core.controller.BaseController;
import com.geovis.emergency.spd.biz.basic.service.IBasicDutyOrgService;
import com.geovis.emergency.spd.biz.system.wrapper.ForestNodeMerger;
import com.geovis.emergency.spd.entity.basic.entity.BasicDutyOrg;
import com.geovis.emergency.spd.entity.basic.pojo.dto.BasicDutyOrgSaveDTO;
import com.geovis.emergency.spd.entity.basic.pojo.dto.BasicDutyOrgTreeDTO;
import com.geovis.emergency.spd.entity.basic.pojo.dto.BasicDutyOrgUpdateDTO;
import com.geovis.emergency.spd.entity.basic.pojo.vo.BasicDutyOrgVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  基础模块-组织架构接口
 * </p>
 *
 * @author Lonsho
 * @version 1.0
 * @date 2022/7/11
 */
@RestController
@RequestMapping("/basic/basicDutyOrg")
@RequiredArgsConstructor
@Api(value = "基础模块-组织架构接口", tags = "基础模块-组织架构接口")
@Slf4j
@Validated
public class BasicDutyOrgController extends BaseController<IBasicDutyOrgService> {

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = SwaggerConstants.SAVE_VALUE, notes = SwaggerConstants.SAVE_NOTES)
    @PostMapping({"/save"})
    public Result<String> save(@Validated @RequestBody BasicDutyOrgSaveDTO saveDTO) {
        BasicDutyOrg basicDutyOrg = BeanUtil.toBean(saveDTO, BasicDutyOrg.class);
        setBaseEntityAttr(basicDutyOrg);
        baseService.save(basicDutyOrg);
        return Result.ok();
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = SwaggerConstants.UPDATE_VALUE, notes = SwaggerConstants.UPDATE_NOTES)
    @PostMapping({"/update"})
    public Result<String> update(@Validated @RequestBody BasicDutyOrgUpdateDTO updateDTO) {
        BasicDutyOrg basicDutyOrg = baseService.getById(updateDTO.getId());
        Assert.notNull(basicDutyOrg, "id:{},库里没有查询要相关数据");
        BeanUtil.copyProperties(updateDTO, basicDutyOrg);
        setBaseEntityAttr(basicDutyOrg);
        baseService.updateById(basicDutyOrg);
        return Result.ok();
    }
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = SwaggerConstants.GET_BY_ID_VALUE, notes = SwaggerConstants.GET_BY_ID_NOTES)
    @PostMapping("/getDetail/{id}")
    public Result<BasicDutyOrg> getById(@PathVariable("id") String id) {
        return Result.ok(baseService.getById(id));
    }
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "树形列表", notes = "树形列表")
    @PostMapping("/tree")
    public Result<List<BasicDutyOrgVO>> tree(@RequestBody BasicDutyOrgTreeDTO treeDTO) {
        return Result.ok(ForestNodeMerger.merge(baseService.selectList(treeDTO)));
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = SwaggerConstants.REMOVE_BY_ID_LIST_VALUE, notes = SwaggerConstants.REMOVE_BY_ID_LIST_NOTES)
    @ApiImplicitParams({@ApiImplicitParam(name = "idList", value = "删除id的list", required = true, dataType = "java.util.Set", paramType = "body")})
    @PostMapping("/removeByIdList")
    public Result removeByIdList(@NotNull(message = "删除的id集合不能为空") @RequestBody(required = false) Set<String> idList) {
        baseService.removeByIdList(idList);
        return Result.ok();
    }
    private void setBaseEntityAttr(BasicDutyOrg basicDutyOrg){
        if (ObjectUtil.isEmpty(basicDutyOrg.getParentId())) {
            basicDutyOrg.setParentId(CommonConstants.TREE_ROOT_CODE);
            basicDutyOrg.setAncestors(CommonConstants.TREE_ROOT_CODE);
        }else {
            BasicDutyOrg parent = baseService.getById(basicDutyOrg.getParentId());
            if (ObjectUtil.isNotNull(parent)){
                basicDutyOrg.setAncestors(parent.getAncestors()+","+basicDutyOrg.getParentId());
            }
        }
    }
}
